<?php date_default_timezone_set('PRC');
class Task_mod extends CI_Model{
	public function __construct()
	{
		parent::__construct();
		
		$this->load->database();
		$this->load->library('session');
	}
	//增加任务
	public function addtask($uid,$content,$t_rank,$t_ct,$ut_id,$u_send_id=0)
	{
		if($t_ct==1){
			$date = date('Y-m-d',time());
		}else if($t_ct==2){
			$date = date('Y-m-d',strtotime("+1 day"));
		}else{
			$date = date('Y-m-d',strtotime("+2 day"));
		}
		if($t_rank == 5){
			$query=$this->db->query("insert into tms_task (uid,t_name,t_rank,completetime,completedate,ut_id,u_send_id) values('$uid','$content','$t_rank',$t_ct,'$date','$ut_id','$u_send_id')");
			$t_id = mysql_insert_id();
			$query=$this->db->query("insert into tms_meiri_type (uid,t_id,m_time) values('$uid','".$t_id."','".date('Y-m-d',time())."')");
		}else{
			$query=$this->db->query("insert into tms_task (uid,t_name,t_rank,completetime,completedate,ut_id,u_send_id) values('$uid','$content','$t_rank',$t_ct,'$date','$ut_id','$u_send_id')");
		}
		if($query)
		{
			mysql_insert_id();
			return mysql_insert_id();
		}
		else
		{
			return false;
		}
	}
	
	//获取过期任务总数
	public function count_task($uid=''){
		if($uid != ''){
			$uid = " uid='$uid' or ";
		}else{
			$uid = "";
		}
		$query=$this->db->query("select count(*) as c from tms_task where (".$uid."is_rel=1) and status=0  and completetime='4'");
		return $query->row_array();
	}
	//获得某用户的今日的任务
	public function gettotask_ut($ut_id,$completetime='')
	{
		if($completetime != ''){
			$completetime = ' and t.completetime='.$completetime;
		}
		if($ut_id != ''){
			$completetime .= ' and t.ut_id='.$ut_id;
		}
		$query=$this->db->query("select t.*,a.m_username,a.rank,a.lasttime from tms_task AS t left join tms_admin as a on t.uid =a.m_id where status=0 ".$completetime." order by t.t_rank desc,t.t_mtime desc");
		return $query->result_array();
	}
	
	//获得某用户的今日的任务
	public function gettotask($uid,$completetime='',$ut_id='')
	{
		$cdata = $completetime;
		$date = date('Y-m-d',time());
		if($completetime != ''){
			$completetime = ' and t.completetime='.$completetime;
		}
		if($ut_id != ''){
			$completetime .= ' and t.ut_id='.$ut_id;
		}
		/*$sql = "(select t.*,a.m_username,a.rank,a.lasttime from tms_task AS t 
			left join tms_admin as a on t.uid =a.m_id 
			where ((t.uid='$uid' or t.is_rel=1) and t.status=0 ".$completetime.")or(t.u_send_id='$uid') order by t.t_rank desc,t.t_mtime desc)";*/
		
		$sql = "(select t.*,a.m_username,a.rank,a.lasttime from tms_task AS t 
			left join tms_admin as a on t.uid =a.m_id 
			where ((t.uid='$uid' or t.is_rel=1) and t.status=0 ".$completetime." and t.completetime<>5)or(t.u_send_id='$uid' and t.completetime=".$cdata." and t.status=0) order by t.t_rank desc,t.t_mtime desc) 
			union 
			(select t.*,a.m_username,a.rank,a.lasttime from tms_meiri_type as tm
			left join tms_task  AS t  on tm.t_id=t.t_id
			left join tms_admin as a on t.uid =a.m_id 
			where ((t.uid='$uid' or t.is_rel=1) and t.status=0 and tm.m_time='$date' and tm.status=0 and t.completetime=".$cdata.") order by t.t_rank desc,t.t_mtime desc)";
		
		$query=$this->db->query($sql);
		return $query->result_array();
	}
	
	//获得某用户的未完成的任务数量
	public function gettotask_count($uid,$time=0,$ut_id='')
	{
		if($time == 1){
			$completetime = ' and completetime in (1,2)';
		}else{
			$completetime ='';
		}
		if($ut_id != ''){
			$completetime .= ' and ut_id='.$ut_id;
		}
		$query=$this->db->query("select count(*) as c from tms_task where ((uid='$uid' or is_rel=1) and status=0 ".$completetime.")or u_send_id='$uid' order by t_rank desc,t_mtime desc");
		return $query->row_array();
	}
	
	//客户时间分类
	public function gettotask_g_ut($ut_id='')
	{
		$query=$this->db->query("select completetime,completedate from tms_task where status=0 and ut_id=".$ut_id." group by completetime  order by completetime");
		return $query->result_array();
	}
	
	//时间分类
	public function gettotask_g($uid='')
	{
		if($uid != ''){
			$uid = " uid='$uid' or ";
		}else{
			$uid = "";
		}
		$query=$this->db->query("select * from ((select completetime,completedate from tms_task_meiri where (".$uid."is_rel=1) and status=0) union (select completetime,completedate from tms_task where (".$uid."is_rel=1) and status=0))as a group by a.completetime");
		return $query->result_array();
	}
	
	//修改任务时间
	public function updatetotask()
	{
		$query=$this->db->query("select t_id,completetime,completedate from tms_task where status=0 order by completetime");
		$arr = $query->result_array();
		$sql_k = 0;
		foreach($arr as $val){
			switch($val['completetime']){
				case 1:
					if($val['completedate'] < date('Y-m-d',time())){
						$sql = "update tms_task set completetime='4' where t_id=".$val['t_id'];$sql_k = 1;
					}break;
				case 2:
					if($val['completedate'] == date('Y-m-d',time())){
						$sql = "update tms_task set completetime='1' where t_id=".$val['t_id'];$sql_k = 1;
					}else if($val['completedate'] < date('Y-m-d',time())){
						$sql = "update tms_task set completetime='4' where t_id=".$val['t_id'];$sql_k = 1;
					}break;
				case 3:
					if($val['completedate'] == date('Y-m-d',time())){
						$sql = "update tms_task set completetime='1' where t_id=".$val['t_id'];$sql_k = 1;
					}else if($val['completedate'] == date('Y-m-d',strtotime("+1 day"))){
						$sql = "update tms_task set completetime='2' where t_id=".$val['t_id'];$sql_k = 1;
					}else if($val['completedate'] < date('Y-m-d',time())){
						$sql = "update tms_task set completetime='4' where t_id=".$val['t_id'];$sql_k = 1;
					}break;
			}
			if($sql_k == 1){
				$query=$this->db->query($sql);
			}
		}
	}
	
	
	//获得某用户的常用链接
	public function gettourl($uid=0)
	{
		if($uid != 0){
			$uid= 'uid='.$uid.' or ';
		}else{
			$uid='';
		}
		
		$query=$this->db->query("select * from tms_task where (".$uid."is_rel=1)  and t_rank=5 order by t_rank desc,t_mtime desc");
		return $query->result_array();
	}
	
	//修改人物状态
	public function changestatus($t_id,$status,$tab)
	{
		$u_id=$this->session->userdata('u_id');
		if($tab == 5){
			if($status==1){
				$this->db->query("update tms_meiri_type set status='$status' where t_id='$t_id' and m_time='".date('Y-m-d',time())."'");
				$this->db->query("insert into tms_meiri_type(uid,t_id,m_time) value('$u_id','$t_id','".date('Y-m-d',strtotime("+1 day"))."')");
			}else{
				$this->db->query("update tms_meiri_type set status='$status' where t_id='$t_id' and m_time='".date('Y-m-d',time())."'");
				$this->db->query("delete from tms_meiri_type where t_id='$t_id' and m_time='".date('Y-m-d',strtotime("+1 day"))."'");
			}
		}else{
			if($status==1)
			{
				$this->db->query("update tms_task set status='$status',t_completedtime=now() where t_id='$t_id'");
			}
			else
			{
				$this->db->query("update tms_task set status='$status' where t_id='$t_id'");
			}
		}

	} 
	//添加人物描述
	public function addtms_desc($t_id,$contents)
	{

		$query=$this->db->query("insert into tms_task_desc (tid,d_content) values('$t_id','$contents')");
	}
	//获得任务的所有描述
	public function getall_desc($t_id)
	{

		$query=$this->db->query("select * from tms_task_desc where tid='$t_id'");
		return $query->result_array();
	}
	//获得所有用户
	public function getalluser($u_key=0)
	{
		if($u_key ==1){
			$where =  "where u_key=1";
		}else{
			$where =  "";
		}
		$query=$this->db->query("select * from tms_admin ".$where);
		return $query->result_array();
	
	}
	//获得所有的未完成的任务
	public function getalltask($completetime='',$ut_id='')
	{
		if($completetime != ''){
			$completetime = ' and t.completetime='.$completetime;
		}
		if($ut_id != ''){
			$completetime .= ' and t.ut_id='.$ut_id;
		}
		$date = date('Y-m-d',time());
		$query=$this->db->query("select t.*,a.m_id,a.m_username,a.rank,a.lasttime from tms_task as t left join tms_admin as a on t.uid =a.m_id where (status=0 ".$completetime.") or (t.completetime =5 and t.completedate < $date) order by t.t_rank desc,t.t_mtime desc");
		return $query->result_array();
	}
	
	//获得所有的未完成的任务分类
	public function getalltask_g()
	{
		$query=$this->db->query("select t.completetime,t.completedate from tms_task as t left join tms_admin as a on t.uid =a.m_id where status=0 group by t.completetime  order by t.completetime");
		return $query->result_array();
	}
	
	//获得某用户已完成的任务
	public function getdonetask($uid)
	{

		$time=date('Y-m-d');
		//$query=$this->db->query("select * from tms_task where (uid='$uid' and t_mtime>'$time') or is_rel=1 order by t_mtime desc");
		$query=$this->db->query("select * from tms_task where (uid='$uid' or is_rel=1) and status=1  order by t_mtime desc");
		return $query->result_array();
	}
	//获得所有员工已完成的任务
	public function getdonealltask()
	{

		$query=$this->db->query("select t.*,a.* from tms_task as t left join tms_admin as a on t.uid =a.m_id where status=1 order by t.t_mtime desc");
		return $query->result_array();
	}
	//获得默认的职责
	public function getduty($uid)
	{

		$query=$this->db->query("select * from tms_duty where uid='$uid'");
		return $query->first_row();
	}
	//修改职责
	public function modifyduty($uid,$content)
	{

		$query=$this->db->query("update tms_duty set duty='$content' where uid='$uid'");
		return $query;
	}
}
?>